// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainForm.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2010
// </copyright>
// <summary>
//   Defines the MainForm type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MtgMC
{
  #region Namespace Imports

  using System;
  using System.Globalization;
  using System.Linq;
  using System.Windows.Forms;

  using DataModel;

  #endregion


  /// <summary>
  /// Describes and manipulates the main form.
  /// </summary>
  public partial class MainForm : Form
  {
    #region Constants and Fields

    private readonly ApplicationData _applicationData;

    #endregion


    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="MainForm"/> class.
    /// </summary>
    /// <param name="applicationData">
    /// The application data.
    /// </param>
    public MainForm(ApplicationData applicationData)
    {
      _applicationData = applicationData;
      InitializeComponent();

      UpdateControls();
    }

    #endregion


    #region Methods

    private void EditPersonalDetailsMenuItemClick(object sender, EventArgs e)
    {
      var form = new PlayerDetailsForm(_applicationData.Player, _applicationData);

      form.ShowDialog();
    }


    private void EditTournamentsMenuItemClick(object sender, EventArgs e)
    {
      var form = new TournamentsForm(_applicationData);
      form.ShowDialog();

      UpdateControls();
    }


    private void NewMatchMenuItemClick(object sender, EventArgs e)
    {
      var matchDialog = new MatchForm(_applicationData);
      DialogResult dialogResult = matchDialog.ShowDialog();

      if (dialogResult == DialogResult.Cancel || !matchDialog.IsOkButtonClicked)
      {
        return;
      }

      ProcessNewMatch(matchDialog.Match);

      UpdateControls();
    }


    private void ProcessNewMatch(Match match)
    {
      var tournament = match.Event as Tournament;

      if (tournament != null)
      {
        if (!_applicationData.Tournaments.Contains(tournament))
        {
          _applicationData.Tournaments.Add(tournament);
        }

        if (!tournament.Opponents.Any(o => o.Opponent == match.Opponent))
        {
          var opponentInfo = new TournamentOpponentInfo { Opponent = match.Opponent, Deck = match.OpponentDeck };

          tournament.Opponents.Add(opponentInfo);
        }
      }

      if (!_applicationData.Locations.Contains(match.Event.Location))
      {
        _applicationData.Locations.Add(match.Event.Location);
      }

      if (!_applicationData.Player.Decks.Contains(match.Event.Deck) && !match.Event.Deck.Format.IsLimitedFormat())
      {
        _applicationData.Player.Decks.Add(match.Event.Deck);
      }

      if (!_applicationData.Players.Contains(match.Opponent))
      {
        _applicationData.Players.Add(match.Opponent);
      }

      if (!match.Opponent.Decks.Contains(match.OpponentDeck) && !match.OpponentDeck.Format.IsLimitedFormat())
      {
        match.Opponent.Decks.Add(match.OpponentDeck);
      }

      _applicationData.PlayedMatches.Add(match);
    }


    private void QuitMenuItemClick(object sender, EventArgs e)
    {
      Close();
    }


    private void SaveStateMenuItemClick(object sender, EventArgs e)
    {
      Program.SaveState();

      MessageBox.Show(
        "Program state was successfully saved.",
        "State Saved",
        MessageBoxButtons.OK,
        MessageBoxIcon.Asterisk,
        MessageBoxDefaultButton.Button1);
    }


    private void UpdateControls()
    {
      int gamesPlayed = _applicationData.PlayedMatches.Count;
      int gamesWon = _applicationData.PlayedMatches.Where(m => m.WasWon).Count();
      int gamesLost = gamesPlayed - gamesWon;

      _gamesLostLabel.Text = gamesLost.ToString(CultureInfo.CurrentCulture);
      _gamesPlayedLabel.Text = gamesPlayed.ToString(CultureInfo.CurrentCulture);
      _gamesWonLabel.Text = gamesWon.ToString(CultureInfo.CurrentCulture);
      _ongoingTournamentsListBox.DataSource =
        _applicationData.Tournaments.Where(t => t.EndDate >= DateTime.Now.Date).ToList();
    }

    #endregion
  }
}